package com.ht.api.util;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;

public class WxAttachCheckUtil {

//    public static String checkImage(String ossUrl) {
//        try {
//            return resizeImage(ossUrl);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//        return null;
//    }
//
//
//    public static String resizeImage(String originalImagePath) throws IOException {
//
//        try {
//            // 读取原始图片
//            File srcImg = WxFileUtils.downFileFromAli(originalImagePath);
//            Image originalImage  =  javax.imageio.ImageIO.read(new FileInputStream(srcImg));
//            int width = originalImage.getWidth(null);
//            int height = originalImage.getHeight(null);
//            File targetImg = null;
//            if (width > 1080 || height > 10800) {
//                int newWidth = 1080;
//                int newHeight = 10800;
//                //获取缩放比利
//                int scale = calculateScale(width, height, newWidth, newHeight);
//                File fold = Files.createTempDirectory("htjava-temp").toFile();
//                String defaultBaseDir = fold.getPath();
//                targetImg = new File(defaultBaseDir + File.separator + HtUtil.getId() + ".png");
//
//                newWidth  =  ( int ) (width *  scale  /   100.0 );
//                newHeight  =  ( int ) (height  *  scale  /   100.0 );
//                BufferedImage bufferedImage  =   new  BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
//                bufferedImage.getGraphics().drawImage(originalImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH), 0 ,  0 ,  null );
////            JPEGImageEncoder encoder  =  JPEGCodec.createJPEGEncoder(new FileOutputStream(targetImg));
////            encoder.encode( bufferedImage);
//                ImageIO.write(bufferedImage, "png", targetImg);
//            } else {
//                targetImg = srcImg;
//
//            }
//            //上传阿里云 返回阿里云地址
//
//            String ossUrl  = AliyunUtil.upload(targetImg, OSSFileType.OTHER);
//            return ossUrl;
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//
//        return null;
//
//    }
//
//    public static int calculateScale(int originalWidth, int originalHeight, int targetWidth, int targetHeight) {
//        int scaleWidth =   (targetWidth * 100) / originalWidth;
//        int scaleHeight =   (targetHeight * 100) / originalHeight;
//        return Math.min(scaleWidth, scaleHeight);
//    }

}
